<?php
error_reporting(E_ERROR | E_PARSE);
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['un_game'])) {
    $_SESSION['un_game'] = [
        'resources' => 1000, // 联合国可用资源
        'conflicts' => [
            [
                'name' => '地区 A 冲突',
                'severity' => 3 // 冲突严重程度，1 - 5
            ],
            [
                'name' => '地区 B 冲突',
                'severity' => 2
            ]
        ],
        'cooperations' => [
            [
                'name' => '项目 X 合作',
                'progress' => 20 // 合作项目进度，0 - 100
            ]
        ]
    ];
}

// 处理解决冲突操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['resolve_conflict'])) {
    $conflict_index = (int)$_POST['conflict_index'];
    $conflict = $_SESSION['un_game']['conflicts'][$conflict_index];
    $cost = $conflict['severity'] * 100;
    if ($_SESSION['un_game']['resources'] >= $cost) {
        $_SESSION['un_game']['resources'] -= $cost;
        unset($_SESSION['un_game']['conflicts'][$conflict_index]);
        $_SESSION['un_game']['conflicts'] = array_values($_SESSION['un_game']['conflicts']);
        $message = "成功解决 {$conflict['name']}，消耗资源 $cost。";
    } else {
        $message = "资源不足，无法解决 {$conflict['name']}。";
    }
}

// 处理促进合作操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['promote_cooperation'])) {
    $cooperation_index = (int)$_POST['cooperation_index'];
    $cost = 200;
    if ($_SESSION['un_game']['resources'] >= $cost) {
        $_SESSION['un_game']['resources'] -= $cost;
        $_SESSION['un_game']['cooperations'][$cooperation_index]['progress'] += 30;
        if ($_SESSION['un_game']['cooperations'][$cooperation_index]['progress'] >= 100) {
            unset($_SESSION['un_game']['cooperations'][$cooperation_index]);
            $_SESSION['un_game']['cooperations'] = array_values($_SESSION['un_game']['cooperations']);
            $message = "成功完成项目 {$cooperation_index}，消耗资源 $cost。";
        } else {
            $message = "成功推动项目 {$cooperation_index} 进度，消耗资源 $cost。";
        }
    } else {
        $message = "资源不足，无法促进合作。";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟联合国主席游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>模拟联合国主席游戏</h1>
    <p>联合国可用资源: <?php echo $_SESSION['un_game']['resources']; ?></p>

    <h2>当前冲突情况</h2>
    <ul>
        <?php foreach ($_SESSION['un_game']['conflicts'] as $index => $conflict): ?>
            <li>
                <?php echo $conflict['name']; ?>（严重程度: <?php echo $conflict['severity']; ?>）
                <form method="post">
                    <input type="hidden" name="conflict_index" value="<?php echo $index; ?>">
                    <button type="submit" name="resolve_conflict">解决冲突</button>
                </form>
            </li>
        <?php endforeach; ?>
    </ul>

    <h2>当前合作项目情况</h2>
    <ul>
        <?php foreach ($_SESSION['un_game']['cooperations'] as $index => $cooperation): ?>
            <li>
                <?php echo $cooperation['name']; ?>（进度: <?php echo $cooperation['progress']; ?>%）
                <form method="post">
                    <input type="hidden" name="cooperation_index" value="<?php echo $index; ?>">
                    <button type="submit" name="promote_cooperation">促进合作</button>
                </form>
            </li>
        <?php endforeach; ?>
    </ul>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>
</body>

</html>